//
//  RSWDataOperation+Attendance.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 17/1/2.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "RSWDataOperation+Attendance.h"
#import "RSWRESTService+Attendance.h"

@implementation RSWDataOperation (Attendance)

-(void)getMyAttendance:(NSInteger)pageIndex pageSize:(NSInteger)pageSize completion:(void(^)(NSInteger nResult,NSArray *list,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
      {
          [self pullRefreshViewBegin];
          
          XLGResult *r = nil;
          RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
          if(ser != nil)
          {
              NSString *token = [XLGContext context].m_token;
              
              r = [ser getMyAttendanceToken:token pageIndex:pageIndex pageSize:pageSize];
              
              [self detachDataOperationManager:@[ser]];
          }
          
          [self requestResult:r.outResult output:r.outObj error:r.outError completion:completion];
          
          [self pullRefreshViewEnd];
      }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,nil,nil);
}

NSString *const RSWDataOperationAttendanceAddNotification = @"RSWDataOperationAttendanceAddNotification";
-(void)addAttendance:(AttendanceInfo*)info completion:(void(^)(NSInteger nResult,BOOL result,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
      {
          [self progressViewBeginWithTip:@"提交中，请稍候"];
          
          XLGResult *r = nil;
          BOOL nResult = YES;
          NSString *token = [XLGContext context].m_token;
          RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
          if(ser != nil)
          {
              r = [ser addAttendance:info token:token];
              [self detachDataOperationManager:@[ser]];
              
              if(r.outResult == YES)
              {
                  NSString *value = [r.outObj stringValue];
                  
                  NSArray *paras = [NSArray arrayWithObject:[NSString stringWithFormat:@"&modelid=%@",value]];
                  NSArray *fileIdArray = [self uploadImage:info.images paras:paras];
                
                  if(fileIdArray.count > 0 && (fileIdArray.count != info.images.count))
                  {
                      nResult = NO;
                  }
                  
                  //[NSNotificationCenter postNotification:RSWDataOperationAttendanceAddNotification userInfo:@{@"1":info}];
              }
          }
          
          [self requestResult:r.outResult output:r.outObj error:r.outError completion:^(NSInteger nResult, id output, NSError *error)
           {
               completion(nResult,nResult,error);
           }];
          
          [self progressViewEnd];
      }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,NO,nil);
}

-(void)getMyAttendanceTypeCompletion:(void(^)(NSInteger nResult,NSArray *list,NSError *error))completion
{
    EDOMRequestStateType stateType = [super requestAsyncBlock:^
      {
          [self pullRefreshViewBegin];
          
          XLGResult *r = nil;
          RSWRESTService *ser = [[RSWRESTService alloc] initWithDataOperationManagerDelegate:nil AttachDOM:self];
          if(ser != nil)
          {
              NSString *token = [XLGContext context].m_token;
              
              r = [ser getMyAttendanceTypeToken:token];
              
              [self detachDataOperationManager:@[ser]];
          }
          
          [self requestResult:r.outResult output:r.outObj error:r.outError completion:completion];
          
          [self pullRefreshViewEnd];
      }];
    if(stateType == EDOMRequestStateTypeCanNotRequest)
        completion(TERMINATEREADWRITE,nil,nil);
}

@end
